
import { Button, Row, Col, Icon, message, Modal } from 'antd';
import { WeaTab, WeaProgress, WeaLocaleProvider } from 'ecCom';
import { inject, observer } from "mobx-react";
import { toJS } from 'mobx';
// import { Condition } from '../list/listCondition';
import { WeaTableNew } from 'comsMobx';
import ShareDialog from '../dialog/shareDialog'
import ExchangeDialog from '../dialog/exchangeDialog'
import TaskInfoDialog from '../dialog/taskDialog'
import ModifyLogDialog from "../dialog/modifyLogDialog"
import { TabComponent as TabSelf } from './tabComponent';
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;

@observer
export default class TaskSub extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
        taskid: "",
        coWorkVisiable: false,
        workPlanVisible: false
    }

  }
  componentDidMount(){
      
    if(!window.weaWorkplan && !window.weaCowork){
        eventRegister.loadModule('f_workplan', ()=> {
            eventRegister.loadModule('f_cowork', ()=> {
            }, ()=> {
                // 加载文件失败 的业务处理， 网络超时，没文件之类
                alert("加载协作模块失败，请联系系统管理员！");
            });
        }, ()=> {
            // 加载文件失败 的业务处理， 网络超时，没文件之类
            alert("加载日程模块失败，请联系系统管理员！");
        });
        return;
    }
    if(window.weaWorkplan && !window.weaCowork){
        eventRegister.loadModule('f_cowork', ()=> {
        }, ()=> {
            // 加载文件失败 的业务处理， 网络超时，没文件之类
            alert("加载协作模块失败，请联系系统管理员！");
        });
    }else{
        eventRegister.loadModule('f_workplan', ()=> {
        }, ()=> {
            // 加载文件失败 的业务处理， 网络超时，没文件之类
            alert("加载协作模块失败，请联系系统管理员！");
        });
    }

    }
    componentWillReceiveProps(nextProps) {

    }
    render() {
        const { contentStore, account, showTab = true, callBack } = this.props;
        const { showSearchAd, taskSubStore, tasksubform, exchangeStore, taskInfoStore, logVisible, logTableStore, reLoad, prjid } = contentStore;
        const formParams = tasksubform.getFormParams() || {};
        return (
            <div >
                {showTab && <TabSelf ecId={`${this && this.props && this.props.ecId || ''}_TabSelf@k3vfo8`} contentStore={contentStore} />}
                <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@6j52jl`}
                    comsWeaTableStore={taskSubStore}
                    hasOrder={true}
                    needScroll={true}
                    register_table={() => reLoad(taskSubStore)}
                    getColumns={c => this.reRenderColumns(c)}
                    onOperatesClick={this.onOperatesClick.bind(this)}
                />
                <ShareDialog ecId={`${this && this.props && this.props.ecId || ''}_ShareDialog@airjmf`} contentStore={contentStore} />
                <ExchangeDialog ecId={`${this && this.props && this.props.ecId || ''}_ExchangeDialog@jjhite`} contentStore={exchangeStore} />
                <TaskInfoDialog ecId={`${this && this.props && this.props.ecId || ''}_TaskInfoDialog@mjb68p`} contentStore={taskInfoStore} prjid={prjid} callBack={callBack}/>
                <ModifyLogDialog ecId={`${this && this.props && this.props.ecId || ''}_ModifyLogDialog@icfrus`} ref="modifyLogDialog" title={getLabel(83905, "任务修改记录")} tableStore={logTableStore} />
                {window.weaCowork && window.weaCowork.com && window.weaCowork.com.CreateCowork && this.state.coWorkVisiable && this.state.taskid !== "" && (
                    <window.weaCowork.com.CreateCowork ecId={`${this && this.props && this.props.ecId || ''}_CreateCowork@wt2c6x`}
                        initAddQuery={
                            {
                                relatedType: "task",
                                ids: this.state.taskid
                            }
                        }
                        onCancel={() => {
                            this.setState({
                                coWorkVisiable: false
                            });
                        }}
                    />)
                }

                    {
                        window.weaWorkplan && window.weaWorkplan.com && window.weaWorkplan.com.WorkPlanCreate &&
                        (<window.weaWorkplan.com.WorkPlanCreate ecId={`${this && this.props && this.props.ecId || ''}_WorkPlanCreate@mvwf2h`}
                            type={"create"}//新建为'create',查看为'preview',编辑edit,共享share
                            visible={this.state.workPlanVisible}//显隐受控
                            doClose={() => { this.setWorkPlanVisible(false); }}//关闭回调
                            onlyClose={() => { this.setWorkPlanVisible(false) }}//关闭回调,只做关闭操作
                            workPlanId={""} //日程id，1查看日程时用到
                            createConditionParams={[]}//创建的默认值日期和时间,没有传[]
                            activeKey={""} //查看页面显示的面板1是基本信息，7是相关交流
                            //{(key) => { }}//查看页面切换面板的回调
                            selectUser={account && account.userid}//新建日程的创建人id
                            workPlanTypeOptions={"2"}//客户模块的新建日程，普通新建传'',客户新建传3
                            crmIDs={[]}//通过客户模块新建日程，并自动带出相关客户,[{'id':'','name':''}]
                            projectid={prjid}
                            taskid={this.state.taskid}
                            description={""}//客户模块需求，支持新建时传入的'基本信息-内容'
                        />)
                    }
        </div>)
    }

    // getAdButtons = () => {
    //     const {contentStore} = this.props;
    //     const {getTaskSubList,setShowSearchAd,clearFormFields} = contentStore;
    //     return [
    //         (<Button type="primary" onClick={()=>{getTaskSubList();setShowSearchAd(false);}}>{getLabel(197,"搜索")}</Button>),
    //         (<Button type="ghost" onClick={()=>{clearFormFields();}}>{getLabel(2022,"重置")}</Button>),
    //         (<Button type="ghost" onClick={()=>{setShowSearchAd(false)}}>{getLabel(201,"取消")}</Button>)
    //     ];
    // }
    reRenderColumns(columns) {
        columns.forEach((c,i) => {
            if (c.dataIndex == 'finish') {
                c.render = function (text, record) {
                    return <span className='wea-prj-progressStyle' >
                        <WeaProgress ecId={`${this && this.props && this.props.ecId || ''}_WeaProgress@b8gzd3@${i}`} percent={record.finish || 0} strokeColor={record.finishspan} />
                    </span>
                }
            } else {
              c.render = function(text, record){
                  let valueSpan = record[c.dataIndex + "span"] !== undefined ? record[c.dataIndex + "span"] : record[c.dataIndex];
                  return <span dangerouslySetInnerHTML={{__html: valueSpan}}></span>
                }
            }
        })
        return columns;
    }
    onOperatesClick(record,index,operate,flag){
        const {contentStore:{shareStore,exchangeStore,taskInfoStore,viewModifyLog},callBack} = this.props;
        let _href = operate && operate.href ? operate.href : "";
		let fn = _href.replace("javascript:","");
		fn = fn.substring(0,fn.indexOf('('));
        let that = this;
        // saveCapitalId(record.randomFieldId);  //保存资产id
        if(fn != ""){
			if("onShare"==fn){  //共享
                shareStore.handleShareDialog(true,"task",record.randomFieldId,{})
            }else if('onNewCowork' == fn){   //新建协作
                this.setState({ taskid: record.randomFieldId, coWorkVisiable: true });
                const coworkStoreMap = window.weaCowork.store && window.weaCowork.store.coworkStoreMap;
                const cowortore = coworkStoreMap && coworkStoreMap.getMapStore(3);
                cowortore && cowortore.setCreateState({ showNewCowork: true, createOrEdit: 0 });
            }else if('onNewWorkplan' == fn){   //新建日程
                this.setState({ taskid: record.randomFieldId, workPlanVisible: true });
            }else if('onApprove' == fn){   //
                                
            }else if('onReject' == fn){   //
                                                
            }else if('onEdit' == fn){   //编辑
                taskInfoStore.handleDialog(true,"edit",record.randomFieldId);                                  
            }else if('onLog' == fn){   //修改记录
                viewModifyLog(record.randomFieldId);
                this.refs.modifyLogDialog.setVisible(true);
            }else if('onDiscuss' == fn){   //相关交流
                exchangeStore.handleExchangeDialog(true,"task",record.randomFieldId,{})
            }else if('onDel' == fn){   //删除
                taskInfoStore.delTask("del",record.randomFieldId,callBack); 
            }
        }
    }

    setWorkPlanVisible = (bool) => {
        this.setState({ workPlanVisible: bool })
    }
}

